package my.ts.code.net;

import java.util.Vector;

import my.ts.code.KLog;

public class OutputThread extends Thread {
	public boolean quit = false;
	private SocketHandler socket;
	private String message;
	private Vector<String> messageQueue = new Vector<String>();

	public OutputThread(SocketHandler socket) {
		this.socket = socket;
		this.start();
		KLog.log("-------- Started output thread");
	}

	public synchronized void queueMessage(String str) {
		messageQueue.addElement(str);
		notify();
	}

	public synchronized String getNextPacket() throws InterruptedException {
		while (messageQueue.size() == 0)
			wait();
		String ym = messageQueue.elementAt(0);
		messageQueue.removeElementAt(0);
		return ym;
	}

	public void run() {
		try {
			while (!quit) {
				String mess = getNextPacket();
				socket.sendMessage(mess);
				//
				Thread.sleep(100);
			}
		} catch (Exception e) {
			KLog.log(e.getMessage());
		}

	}

	public void stopThread() {
		quit = true;
	}
}
